namespace ghost
{
    export class CountComponent extends GameUIBase
    {
        public back:GYSprite;
        public back2:GYSprite;
        public countLab:Laya.Text;
        private _callBack:Function;
        private _thisObj:any;
        private _endTime:number;
        private _leftTime:number;
        private _warn:boolean;
        private _flash:boolean;
        constructor()
        {
            super();
            let s= this;
            s.width = GameManager.DESIGN_WIDTH;
            s.height = GameManager.DESIGN_HEIGHT;
            s.back = new GYSprite;
            s.back.texture = Main.instance.getRes(Conf.scene + "tip.png");
            s.back.y = 300;
            s.addChild(s.back);
            s.countLab = new Laya.Text;
            s.countLab.x = 411;
            s.countLab.y = 80;
            s.countLab.fontSize = 30;
            s.countLab.color = "#ee3f54";
            s.countLab.stroke = 6;
            s.countLab.strokeColor = "#250405";
            s.back.addChild(s.countLab);

            s.back2 = new GYSprite;
            s.back2.texture = Main.instance.getRes(Conf.scene + "tip2.png");
            s.addChild(s.back2);
            s.back2.x = 0;
            s.back2.y = 300;
            s.back2.visible = false;
            s.mouseEnabled = s.mouseThrough =true;
        }
        public resize():void
        {
            let s= this;
            s.back2.y = s.back.y = s._module.frameBound.y + 180;
            s.back2.x = s.back.x = 0;
        }
        public show(pr:Laya.Sprite=null, mask:boolean=true, tween:boolean=true, callBack:Function=null, thisObj:any=null):void
        {
            let s= this;
            super.show(pr,mask, tween);
            s._callBack = callBack;
            s._thisObj = thisObj;
            s._endTime = SceneManager.instance.timeTick + Main.gameConfig.startCount;
            Laya.timer.loop(1000, s, s.loop);
            s._warn = false;
            s._flash = false;
            s.back2.visible = false;
        }
        private loop():void
        {
            let s= this;
            let t:number = SceneManager.instance.timeTick;
            let leftTime:number;
            s._leftTime = leftTime = s._endTime - t;     
            if(s._leftTime < Main.gameConfig.startWarnTime && !s._warn)       
            {
                s._warn = true;
                s.warnSound();
            }
            if(s._leftTime < Main.gameConfig.startFlashWarnTime && !s._flash)       
            {
                s._flash = true;
                s.flash();
            }
            if(leftTime <= 0)
            {
                leftTime = 0;
                Laya.timer.clear(s, s.loop);
                s.back.visible = false;
                s.back2.visible = true;                
                s.back2.alpha = 0;           
                GYTween.to(s.back2,[TweenData.getInstance("alpha", 1)],1000,0,s,()=>{                    
                    s.back2.alpha = 1;
                    GYTween.to(s.back2,[TweenData.getInstance("alpha", 0)],1000,1500,s,()=>{
                        if(s._callBack!=null)
                        {                            
                            s._callBack.call(s._thisObj);
                            s._callBack = s._thisObj = null;
                            s.back2.visible = false;
                        } 
                    });
                });
            }            
            s.countLab.text = `${Math.floor(leftTime/1000) % 60}秒`;
        }
        /**闪烁提醒*/
        public flash(alpha:number=0.5):void{
            Laya.Tween.to(this.back, {alpha:alpha}, 200, null, Laya.Handler.create(this, ()=>{
                if(this._leftTime <= 0 || !this.displayedInStage)
                    return;
                this.flash(this.back.alpha>=1?0.5:1);
            }));
        }
        /**语音提醒*/
        public warnSound():void{
            SoundManager.instance.play(`${Conf.sound}warn.mp3`, 0, 1,null,null,SoundData.BACKGROUND_SOUND);
        }
    }
}